package com.ctp.ui.toolarea;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.ctp.bean.FormBean;
import com.ctp.bean.MarketBean;
import com.ctp.controller.manager.TraderManager;
import com.ctp.socket.util.TraderUtil;
import com.ctp.thread.factory.ThreadFactory;
import com.ctp.thread.passive.ITraderProcessor;

public class TraderToolBox extends JDesktopPane {

	private static final long serialVersionUID = 1L;

	private boolean longInd = false;

	private JTextField priceText = new JTextField();

	private TraderManager traderManager = TraderManager.instance();

	private ITraderProcessor traderProcessor = ThreadFactory
			.getTraderProcessor();

	private MarketBean market = traderManager.getMarkets()[0];

	public TraderToolBox(boolean longInd) {
		this.longInd = longInd;
		this.setBorder(BorderFactory.createLineBorder(Color.GRAY));

		this.setSize(315, 30);

		initContrLabel();
		initPriceField();

		initOpenButton();
		initOpenForceButton();
	}

	private void initContrLabel() {
		JLabel label = new JLabel();

		label.setBounds(10, 5, 40, 20);
		label.setForeground(Color.WHITE);

		if (longInd) {
			label.setText("多头：");
			this.setBackground(OPEN_COLOR);
		} else {
			label.setText("空头：");
			this.setBackground(CLOSE_COLOR);
		}

		this.add(label);
	}

	private void initPriceField() {
		priceText.setBounds(55, 5, 80, 20);
		priceText.setHorizontalAlignment(JTextField.CENTER);

		priceText.addMouseWheelListener(new MouseWheelListener() {
			public void mouseWheelMoved(MouseWheelEvent event) {
				int move = event.getUnitsToScroll() / event.getScrollAmount();

				try {
					int price = Integer.parseInt(priceText.getText());
					priceText.setText(Integer.toString(price - move));
				} catch (Exception e) {
				}
			}
		});

		priceText.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent event) {
				if (event.getButton() == MouseEvent.BUTTON3) {
					double price = market.getLastPrice();
					int intPrice = TraderManager.getIntPrice(price);

					priceText.setText(Integer.toString(intPrice));
				}
			}

			public void mouseEntered(MouseEvent e) {
			}

			public void mouseExited(MouseEvent e) {
			}

			public void mousePressed(MouseEvent e) {
			}

			public void mouseReleased(MouseEvent e) {
			}
		});

		this.add(priceText);
	}

	private void initOpenButton() {
		JButton button = new JButton("开仓");

		button.setBounds(145, 5, 60, 20);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				int price = 0;

				try {
					price = Integer.parseInt(priceText.getText());
				} catch (Exception e) {
					return;
				}

				execOpenPosition(price, TraderManager.VOLUME_UNIT);
			}
		});

		this.add(button);
	}

	private void initOpenForceButton() {
		JButton button = new JButton("市价开仓");

		button.setBounds(215, 5, 90, 20);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				double price = longInd ? market.getUpperPrice() : market
						.getLowerPrice();

				execOpenPosition(price, TraderManager.VOLUME_UNIT);
			}
		});

		this.add(button);
	}

	private void execOpenPosition(double price, int volume) {
		String contr = market.getContrName();
		String[] ret = TraderUtil.openPos(contr, longInd, price, volume, 0);

		if (ret != null) {
			FormBean formBean = new FormBean(ret[0]);
			formBean.setFormBasicInfo(contr, longInd, true, price, volume);

			traderManager.addFormBySeq(formBean);
			traderProcessor.insertNewForm(formBean, null);
		}
	}

	private static final Color OPEN_COLOR = new Color(170, 20, 20);

	private static final Color CLOSE_COLOR = new Color(10, 110, 10);

}
